window.onload = function () {

    // 所有面板的声明
    allPage = {
        'userManage': $('#userManage'),
        'roomManage': $('#roomManage'),
        'mallManage': $('#mallManage'),
        'moneyTopTo': $('#moneyTopTo'),
        'drawApply': $('#drawApply'),
        'roomApply': $('#roomApply'),
        'roomBuild': $('#roomBuild'),
        'orderManage': $('#orderManage'),
        'loading': $('#loading'),
    };

    userNum = 0;
    roomNum = 0;
    drawNum = 0;
    userInfo = {
        ssid: 'aaf34f7f-5b06-4854-9e45-ed756ec14054',
        uiid: "A493762310",
    };

    //需要获取保存的元素
    //userManage
    userInfoTableTitle = $('#userInfoTableTitle');
    userInfoSample = getElementSample('userInfoSample');

    //roomManage
    roomInfoTableTitle = $('#roomInfoTableTitle');
    roomInfoSample = getElementSample('roomInfoSample');
    roomType = {
        type1: '红包接龙',
        type2: '尾数相加',
        type3: '红包扫雷',
    }

    //mallManage
    goodsInfoTableTitle = $('#goodsInfoTableTitle');
    goodsInfoSample = getElementSample('goodsInfoSample');

    //drawApply
    drawInfoTableTitle = $('#drawInfoTableTitle');
    drawApplySample = getElementSample('drawApplySample');

    //roomApply
    roomApplyTableTitle = $('#roomApplyTableTitle');
    roomApplySample = getElementSample('roomApplySample');

    //导航栏的监听
    addMyEventListener($('.navBarOne'), 'onclick', navFun);

    refreshUserNum();
    refreshRoomNum();
    refreshDrawNum();

    addMyEventListener($('#userManageBoot .whichPage'), 'onclick', changeUserPageFun);
    addMyEventListener($('#roomManageBoot .whichPage'), 'onclick', changeRoomPageFun);
    addMyEventListener($('#drawApplyBoot .whichPage'), 'onclick', changeDrawPageFun);

    // 登录
    // addMyEventListner($('#loginSubmit'), 'onclick', loginFun);

} // end window.onload

//导航栏的监听函数
function navFun(e) {
    var target = getMyEventMeetsRule(e, '.navBarOne');
    var pageId = target.getAttribute('navFor');
    var allNav = $('.navBarOne');
    for (var i = 0; i < allNav.length; i++) {
        if (target == allNav[i]) {
            removeMyClass(target, 'bgGreen');
            addMyClass(target, 'bgYellow');
        } else {
            removeMyClass(allNav[i], 'bgYellow');
            addMyClass(allNav[i], 'bgGreen');
        }
    }
    $('#mainTitletext').innerHTML = $('.navBarOneText', target).innerHTML;
    showOnePage(allPage[pageId]);
    if (pageId == 'userManage') {
        loadUserInfoPage(1, parseInt($('#userPageSize').value));
    } else if (pageId == 'roomManage') {
        loadRoomInfoPage(1, parseInt($('#roomPageSize').value));
    } else if (pageId == 'mallManage') {
        loadGoodsInfoPage();
    } else if (pageId == 'drawApply') {
        loadDrawInfoPage(1, parseInt($('#drawPageSize').value));
    } else if (pageId == 'roomApply') {
        loadRoomApplyPage();
    }

}





//用户信息页面跳转的监听函数
function changeUserPageFun(e) {
    var tar = e.target;
    var size = parseInt($('#userPageSize').value);
    var start = 1;
    if (tar == $('#userManageBoot #toThePageBtn')) {
        start = parseInt($('#userCurrentPage').value);
    } else {
        start = parseInt(tar.getAttribute('tablePageFor'));
    }

    loadUserInfoPage(start, size);
}

//房间信息页面跳转的监听函数
function changeRoomPageFun(e) {
    var tar = e.target;
    var size = parseInt($('#roomPageSize').value);
    var start = 1;
    if (tar == $('#roomManageBoot #toThePageBtn')) {
        start = parseInt($('#roomCurrentPage').value);
    } else {
        start = parseInt(tar.getAttribute('tablePageFor'));
    }

    loadRoomInfoPage(start, size);
}

//提现申请页面跳转的监听函数
function changeDrawPageFun(e) {
    var tar = e.target;
    var size = parseInt($('#drawPageSize').value);
    var start = 1;
    if (tar == $('#drawManageBoot #toThePageBtn')) {
        start = parseInt($('#drawCurrentPage').value);
    } else {
        start = parseInt(tar.getAttribute('tablePageFor'));
    }

    loadDrawInfoPage(start, size);
}



//加载申请房间请求
function loadRoomApplyPage() {
    var req = {};
    copyObjFields(req, getBaseReq());
    Ajax.post('room/qryapply', req, function (data) {
        var res = JSON.parse(data);
        if (res.retCode == okCode) {
            var list = res.data;
            fillRoomApplyListToTbale(list);
        } else {
            alert(res.retMsg);
        }
    });
}

// 根据申请房间请求数组填充到表格里
function fillRoomApplyListToTbale(list) {
    var con = $('#roomApplyTable');
    con.innerHTML = '';
    con.appendChild(roomApplyTableTitle);
    for (var i = 0; i < list.length; i++) {
        var room = list[i];
        var content = roomApplySample.cloneNode(true);
        $('[name=roomId]', content).innerHTML = room.id;
        $('[name=roomOwner]', content).innerHTML = room.owner;
        $('[name=roomName]', content).innerHTML = room.name;
        $('[name=roomType]', content).innerHTML = roomType['type' + room.type];
        $('[name=roomLevel]', content).innerHTML = room.level;
        $('[name=roomDesc]', content).innerHTML = room.desc;
        $('[name=roomBonusMoney]', content).innerHTML = room.bonusMoney / 100;
        $('[name=roomBonusNum]', content).innerHTML = room.bonusNum;
        $('[name=roomTarget]', content).innerHTML = room.target;
        $('[name=roomCreateTime]', content).innerHTML = timeFormat(room.createTime, 'yyyy-MM-dd');
        con.appendChild(content);
    }
}

//加载商品信息
function loadGoodsInfoPage() {
    var req = {};
    copyObjFields(req, getBaseReq());
    Ajax.post('goods/qry', req, function (data) {
        var res = JSON.parse(data);
        if (res.retCode == okCode) {
            var list = res.data;
            fillgoodsListToTbale(list);
        } else {
            alert(res.retMsg);
        }
    });
}

// 根据商品信息数组填充到表格里
function fillgoodsListToTbale(list) {
    var con = $('#goodsInfoTable');
    con.innerHTML = '';
    con.appendChild(goodsInfoTableTitle);
    for (var i = 0; i < list.length; i++) {
        var goods = list[i];
        var content = goodsInfoSample.cloneNode(true);
        $('[name=goodsId]', content).innerHTML = goods.id;
        $('[name=goodsName]', content).innerHTML = goods.name;
        $('[name=goodsDesc]', content).innerHTML = goods.desc;
        $('[name=goodsPrice]', content).innerHTML = goods.price / 100;
        $('[name=goodsMount]', content).innerHTML = goods.mount;
        $('[name=goodsImgUrl]', content).innerHTML = goods.imgUrl;
        $('[name=goodsStatus]', content).innerHTML = goods.status == '1' ? '上架' : '下架';
        $('[name=goodsCreateTime]', content).innerHTML = timeFormat(goods.createTime, 'yyyy-MM-dd');
        con.appendChild(content);
    }
}





//获取提现申请总数
function refreshDrawNum() {
    var req = getBaseReq();
    Ajax.post("money/drawcount", req, function (data) {
        var res = JSON.parse(data);
        if (res.retCode == okCode) {
            drawNum = parseInt(res.data);
            $('#drawCount').innerHTML = drawNum;
            var pageSize = $('#drawPageSize').value;
            $('#drawPageLast').setAttribute('tablePageFor', Math.ceil(drawNum / pageSize));
        }
    });
}
//加载提现申请信息
function loadDrawInfoPage(start, size) {
    var req = {
        startIndex: start - 1,
        pageSize: size,
    }
    copyObjFields(req, getBaseReq());
    Ajax.post('money/drawlist', req, function (data) {
        var res = JSON.parse(data);
        if (res.retCode == okCode) {
            $('#drawCountStart').innerHTML = req.startIndex * size + 1;
            var pageSize = parseInt($('#drawPageSize').value);
            var drawCount = parseInt($('#drawCount').innerHTML);
            var drawCountEnd = parseInt(req.startIndex * size + size);
            $('#drawCountEnd').innerHTML = drawCountEnd > drawCount ? drawCount : drawCountEnd;
            $('#drawPagePre').setAttribute('tablePageFor', start - 1 < 1 ? 1 : start - 1);
            $('#drawPageLast').setAttribute('tablePageFor', Math.ceil(drawCount / size));
            var pageNum = parseInt($('#drawPageLast').getAttribute('tablePageFor'));
            $('#drawPageNext').setAttribute('tablePageFor', start + 1 <= pageNum ? start + 1 : pageNum);
            $('#drawCurrentPage').value = start;
            var list = res.data;
            fillDrawListToTbale(list);
        } else {
            alert(res.retMsg);
        }
    });
}
// 根据提现申请信息数组填充到表格里
function fillDrawListToTbale(list) {
    var con = $('#drawInfoTable');
    con.innerHTML = '';
    con.appendChild(drawInfoTableTitle);
    for (var i = 0; i < list.length; i++) {
        var draw = list[i];
        var content = drawApplySample.cloneNode(true);
   
        $('[name=uiid]', content).innerHTML = draw.uiid;
        $('[name=money]', content).innerHTML = draw.money;
        $('[name=bankAccunt]', content).innerHTML = draw.bankAccunt;
        $('[name=bankName]', content).innerHTML = draw.bankName;
        $('[name=accuntName]', content).innerHTML = draw.accuntName;
        $('[name=mobilePhone]', content).innerHTML = draw.mobilePhone;
        $('[name=drawCreateTime]', content).innerHTML = timeFormat(draw.createTime, 'yyyy-MM-dd');
        con.appendChild(content);
    }
}




//获取房间总数
function refreshRoomNum() {
    var req = getBaseReq();
    Ajax.post("room/qrycount", req, function (data) {
        var res = JSON.parse(data);
        if (res.retCode == okCode) {
            roomNum = parseInt(res.data);
            $('#roomCount').innerHTML = roomNum;
            var pageSize = $('#roomPageSize').value;
            $('#roomPageLast').setAttribute('tablePageFor', Math.ceil(roomNum / pageSize));
        }
    });
}
//加载房间信息
function loadRoomInfoPage(start, size) {
    var req = {
        startIndex: start - 1,
        pageSize: size,
    }
    copyObjFields(req, getBaseReq());
    Ajax.post('room/qrylist', req, function (data) {
        var res = JSON.parse(data);
        if (res.retCode == okCode) {
            $('#roomCountStart').innerHTML = req.startIndex * size + 1;
            var pageSize = parseInt($('#roomPageSize').value);
            var roomCount = parseInt($('#roomCount').innerHTML);
            var roomCountEnd = parseInt(req.startIndex * size + size);
            $('#roomCountEnd').innerHTML = roomCountEnd > roomCount ? roomCount : roomCountEnd;
            $('#roomPagePre').setAttribute('tablePageFor', start - 1 < 1 ? 1 : start - 1);
            $('#roomPageLast').setAttribute('tablePageFor', Math.ceil(roomCount / size));
            var pageNum = parseInt($('#roomPageLast').getAttribute('tablePageFor'));
            $('#roomPageNext').setAttribute('tablePageFor', start + 1 <= pageNum ? start + 1 : pageNum);
            $('#roomCurrentPage').value = start;
            var list = res.data;
            fillRoomListToTbale(list);
        } else {
            alert(res.retMsg);
        }
    });
}
// 根据房间信息数组填充到表格里
function fillRoomListToTbale(list) {
    var con = $('#roomInfoTable');
    con.innerHTML = '';
    con.appendChild(roomInfoTableTitle);
    for (var i = 0; i < list.length; i++) {
        var room = list[i];
        var content = roomInfoSample.cloneNode(true);
        var bonusDetail = room.bonusDetail.split(',');
        $('[name=roomId]', content).innerHTML = room.id;
        $('[name=roomType]', content).innerHTML = roomType['type' + room.type];
        $('[name=roomDesc]', content).innerHTML = room.desc;
        $('[name=roomOwner]', content).innerHTML = room.owner;
        $('[name=roomLevel]', content).innerHTML = room.level;
        $('[name=roomBonusMoney]', content).innerHTML = parseInt(bonusDetail[0]) / 100;
        $('[name=roomBonusNum]', content).innerHTML = bonusDetail[1];
        if (room.type == 3) {
            $('[name=roomBonusTarget]', content).innerHTML = room.target;
        }
        $('[name=roomCreateTime]', content).innerHTML = timeFormat(room.createTime, 'yyyy-MM-dd');
        con.appendChild(content);
    }
}

//获取用户总数
function refreshUserNum() {
    var req = getBaseReq();
    Ajax.post("user/qrycount", req, function (data) {
        var res = JSON.parse(data);
        if (res.retCode == okCode) {
            userNum = res.data;
            $('#userCount').innerHTML = userNum;
            var pageSize = $('#userPageSize').value;
            $('#userPageLast').setAttribute('tablePageFor', Math.ceil(userNum / pageSize));
        }
    });
}

//加载用户信息
function loadUserInfoPage(start, size) {
    var req = {
        startIndex: start - 1,
        pageSize: size,
    }
    copyObjFields(req, getBaseReq());
    Ajax.post('user/qry', req, function (data) {
        var res = JSON.parse(data);
        if (res.retCode == okCode) {
            $('#userCountStart').innerHTML = req.startIndex * size + 1;
            var pageSize = parseInt($('#userPageSize').value);
            var userCount = parseInt($('#userCount').innerHTML);
            var userCountEnd = parseInt(req.startIndex * size + size);
            $('#userCountEnd').innerHTML = userCountEnd > userCount ? userCount : userCountEnd;
            $('#userPagePre').setAttribute('tablePageFor', start - 1 < 1 ? 1 : start - 1);
            $('#userPageLast').setAttribute('tablePageFor', Math.ceil(userCount / size));
            var pageNum = parseInt($('#userPageLast').getAttribute('tablePageFor'));
            $('#userPageNext').setAttribute('tablePageFor', start + 1 <= pageNum ? start + 1 : pageNum);
            $('#userCurrentPage').value = start;
            var list = res.data;
            fillUserListToTbale(list);
        } else {
            alert(res.retMsg);
        }
    });
}

// 根据用户信息数组填充到表格里
function fillUserListToTbale(list) {
    var con = $('#userInfoTable');
    con.innerHTML = '';
    con.appendChild(userInfoTableTitle);
    for (var i = 0; i < list.length; i++) {
        var user = list[i];
        var content = userInfoSample.cloneNode(true);
        $('[name=uiid]', content).innerHTML = user.uiid;
        $('[name=nickName]', content).innerHTML = user.nickName;
        $('[name=mobilePhone]', content).innerHTML = user.mobilePhone;
        $('[name=createTime]', content).innerHTML = timeFormat(user.createTime, 'yyyy-MM-dd');
        $('[name=money]', content).innerHTML = user.money / 100;
        $('.lockUserBtn', content).setAttribute('act', user.valid);
        if (user.valid == '1') {
            $('.lockUserBtn', content).innerHTML = '冻结';
        } else {
            $('.lockUserBtn', content).innerHTML = '解冻';
        }
        con.appendChild(content);
    }
}

//获取申请房间时的输入信息
    function getApplyRoomInput() {
        var userInfo = getUserInfo();
        var req = {
            ssid: userInfo.ssid,
            owner: userInfo.uiid,
            name: $('#applyRoomName').value,
            type: getRadiosValue($('.roomTypeRadio')),
            id: $('#applyRoomId').value,
            bonusMoney: getSelectValue($('#bonusMoneyCkx')),
            bonusNum: getRadiosValue($('.bonusNumRadio')),
            target: getSelectValue($('#bonusTargetCkx')),
            phone: $('#applyRoomPhone').value,
            wechat: $('#applyRoomWechat').value,
        }
        return req;
        function getSelectValue(selects) {
            return selects.options[selects.selectedIndex].value;
        }
        function getRadiosValue(radios) {
            for (var i = 0; i < radios.length; i++) {
                if (radios[i].checked == true) {
                    return radios[i].value;
                }
            }
        }

    }


function loginFun(e) {
    var loginInfo = getMyInputListObj($('.loginItem>input'));
    console.log(loginInfo);
    if (assertNotEmpty(loginInfo, "nickOrMobile", "账号不能为空")
        || assertNotEmpty(loginInfo, "password", "密码不能为空")) {
        return;
    }
    Ajax.post('user/login', loginInfo, function (data) {
        console.log(data);
        var res = JSON.parse(data);
        if (res.retCode == okCode) {
            sessionStorage.setItem('userInfo', JSON.stringify(res.data));
        } else {
            alert(res.retMsg);
        }
    });
}

function getBaseReq() {
    if (!userInfo) {
        userInfo = getUserInfo();
    }
    return {
        ssid: userInfo.ssid,
        uiid: userInfo.uiid,
    }
}